﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls; 

/// <summary>
/// AllocationAccess 的摘要说明
/// </summary>

using System.Data.SqlClient;
using BookShop;

namespace BookShop
{
    public partial class AllocationController
    {
        public void Update(string AllocationID, DateTime? ShippedDate, string ShipVia, string StoreID)
        {
            Allocation item = new Allocation();
            item.MarkOld();
            item.IsLoaded = true;

            item.AllocationID = AllocationID;

            item.ShippedDate = ShippedDate;

            item.ShipVia = ShipVia;

            item.Status = "reday";

            item.StoreID = StoreID;

            item.Save(UserName);
        }

        public static bool CheckStock(string allcationId, string isbn, string storeId, string max, string stock, bool iscbAmountChecked)
        {
            int amount = 0;
            string sqlstr1, sqlstr2, sqlstr3;
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["BookShop"].ConnectionString);
            conn.Open();
            if (iscbAmountChecked)
            {
                amount = Convert.ToInt32(max) - Convert.ToInt32(stock);
                if (amount < 0) amount = 0;
            }
            sqlstr1 = "select Amount from Stock where WarehouseID = '00' and isbn = '" + isbn + "'";
            sqlstr2 = "select sum(Amount) from AllocationDetail where ISBN = '" + isbn + "' and AllocationID = '" + allcationId + "'";
            sqlstr3 = "select count(*) from AllocationDetail where AllocationID = '" + allcationId + "'";
            SqlCommand sqlcmd1 = new SqlCommand(sqlstr1, conn);
            SqlCommand sqlcmd2 = new SqlCommand(sqlstr2, conn);
            SqlCommand sqlcmd3 = new SqlCommand(sqlstr3, conn);
            if (Convert.ToInt32(sqlcmd3.ExecuteScalar()) > 0)
            {
                string sql2Scalar = sqlcmd2.ExecuteScalar().ToString();
                if (String.IsNullOrEmpty(sql2Scalar))
                {
                    new AllocationDetailController().Insert(allcationId, isbn, amount, storeId);
                    return true;
                }
                else if (Convert.ToInt32(sql2Scalar) + amount < Convert.ToInt32(sqlcmd1.ExecuteScalar()))
                {
                    new AllocationDetailController().Insert(allcationId, isbn, amount, storeId);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                if (amount < Convert.ToInt32(sqlcmd1.ExecuteScalar()))
                {
                    new AllocationDetailController().Insert(allcationId, isbn, amount, storeId);
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }
}